<template>
  <div>
    <common-head title="搜索结果" />
    <div class="wrap">
      <!-- 筛选 -->
      <div class="filter">
        <div @click="orderBySale" :class="[ 'order', 'sale', { active: order.orderBy === 'sale' } ]" >
          <span>销量</span>
          <div :class="[ 'angle', 'up', { active: order.orderBy === 'sale' && order.upDown === 'up' } ]" ></div>
          <div :class="[ 'angle', 'down', { active: order.orderBy === 'sale' && order.upDown === 'down' } ]" ></div>
        </div>
        <div @click="orderByPrice" :class="[ 'order', 'price', { active: order.orderBy === 'price' } ]" >
          <span>价格</span>
          <div :class="[ 'angle', 'up', { active: order.orderBy === 'price' && order.upDown === 'up' } ]" ></div>
          <div :class="[ 'angle', 'down', { active: order.orderBy === 'price' && order.upDown === 'down' } ]" ></div>
        </div>
      </div>
      <!-- /筛选 -->
      <!-- 商品列表 -->
      <div class="wrap-item">
        <div class="itemsa" @click="clickItem(item.id)" v-for="item in items" :key="item.id">
          <div class="itemss">
            <img :src="item.pic" alt="">
            <h3>{{item.name | subStr(10)}}</h3>
            <p class="tlite-p">{{item.characteristic | subStr(21)}}</p>
          </div>
          <div class="pic">
            <p>{{item.minPrice | currency('￥')}}<span>{{item.originalPrice | currency('￥')}}</span></p>
            <van-icon @click.stop="clickAddItem(item)" name="cart" size="28" color="red" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import CommonHead from '@components/CommonHead'
import { fetchItems } from '@api'
import { subStr, currency } from '@/filters'
import addCart from '@/mixins/addCart'
export default {
  mixins: [addCart],
  data () {
    return {
      order: {
        orderBy: 'sale', 
        upDown: 'down' // up 升序 down 降序
      },
      items: [], // 商品列表
      orderBy: 'ordersDown' // 请求接口排序
    }
  },
  created () {
    this.fetchItems()
  },
  methods: {
    clickItem (id) {
      this.$router.push(`/detail/${id}`)
    },
    clickAddItem (item) {
      // 点击加入购物车按钮
      item = { ...item, num: 1, checked: true }
      this._addCart(item)
    },
    fetchItems () {
      fetchItems({
        k: this.$route.query.word,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    orderBySale () {
      // 点击按照销量排序
      if (this.order.orderBy === 'price') {
        this.order.orderBy = 'sale'
        this.order.upDown = 'down'
        this.orderBy = 'ordersDown'
      } else {
        // 按照销量 切换 升序还是降序
        if (this.order.upDown === 'up') {
          this.order.upDown = 'down'
          this.orderBy = 'ordersDown'
        } else {
          this.order.upDown = 'up'
          this.orderBy = 'ordersUp'
        }
      }
      this.fetchItems()
    },
    orderByPrice () {
      // 点击按照价格排序
      if (this.order.orderBy === 'sale') {
        this.order.orderBy = 'price'
        this.order.upDown = 'up'
        this.orderBy = 'priceUp'
        return false
      } else {
        // 按照销量 切换 升序还是降序
        if (this.order.upDown === 'up') {
          this.order.upDown = 'down'
          this.orderBy = 'priceDown'
        } else {
          this.order.upDown = 'up'
          this.orderBy = 'priceUp'
        }
      }
      this.fetchItems()
    }
  },
  filters: {
    subStr,
    currency
  },
  components: {
    CommonHead
  }
}
</script>
<style lang="scss" scoped>
.wrap {
  margin-top: 46px;
  .filter {
    height: 50px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-around;
    .order {
      position: relative;
      color: #333;
      &.active {
        color: #f66;
      }
      .angle {
        width: 0;
        height: 0;
        border: 6px solid transparent;
        position: absolute;
        right: -16px;
        &.up {
          border-bottom-color: #333;
          top: -2px;
          &.active {
            border-bottom-color: #f66;
          }
        }
        &.down {
          border-top-color: #333;
          top: 12px;
          &.active {
            border-top-color: #f66;
          }
        }
      }
    }
  }
}
.wrap-item{
  background: #eee;
  display: flex;
  flex-wrap: wrap;
  padding: 0 5px;
  justify-content: space-between;
  .itemsa{
    width: 49%;
    border-radius: 10px;
    margin-top: 5px;
    background: #fff;
    position: relative;
    .itemss{
      margin-bottom: 40px;
    }
    img{
      width: 100%;
    }
    h3{
      padding: 0 5px;
      margin: 0;
    }
    p{
      padding: 0 5px;
    }
    .pic{
      color: rgb(218, 25, 57);
      display: flex;
      justify-content: space-between;
      font-size: 20;
      padding: 5px;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      p{
        margin-top: 15px;
        font-weight: bold;
      }
      span{
        margin-left: 5px;
        font-size: 12px;
        color: #666;
        text-decoration: line-through;
      }
      .van-icon-cart{
        margin-top: 12px;
      }
    }
  }
}
</style>
